from datetime import datetime

from flask import Blueprint, render_template, request, jsonify, session
from flask_jwt_extended import jwt_required
from flask_login import login_required
from sqlalchemy import and_
from werkzeug.security import generate_password_hash

from applications.common import curd
from applications.common.curd import model_to_dicts, enable_status, disable_status, get_one_by_id
from applications.common.helper import ModelFilter
from applications.common.utils.getopenid import get_openid, token
from applications.common.utils.http import table_api, success_api, fail_api, wxuser_success_api, coupon_api
from applications.common.utils.rights import authorize
from applications.common.utils.validate import xss_escape
from applications.exts import db
from applications.exts.cache import cache
from applications.models import Role, Power, User, CouponUser, Coupon
from applications.models.wx_user import WxUser
from applications.schemas import RoleOutSchema, PowerOutSchema2, WxUserOutSchema, CouponUserOutSchema, CouponOutSchema
from applications.common.utils import upload as upload_curd

usercoupon = Blueprint('usercoupon', __name__, url_prefix='/wxuser/usercoupon')


@usercoupon.route('/addusercoupon', methods=["POST"])
@jwt_required()
def addusercoupon():
    req_json = request.json

    coupon_sn = xss_escape(req_json.get('coupon_sn'))
    openid = xss_escape(req_json.get('openid'))

    res=CouponUser.query.filter(and_(CouponUser.coupon_sn==coupon_sn,CouponUser.openid==openid)).first()
    if res:
        return fail_api(msg='领取失败')
    else:
        couponuser = CouponUser()
        couponuser.coupon_sn = coupon_sn
        couponuser.openid = openid

        db.session.add(couponuser)
        db.session.commit()

        return success_api(msg='领取成功')


@usercoupon.route('/pullusercoupon', methods=["POST", "GET"])
@jwt_required()
def pullusercoupon():
    req_json = request.json
    openid = xss_escape(req_json.get('openid'))

    usercouponlist = []
    couponlist = []

    usercoupons = CouponUser.query.filter(CouponUser.openid == openid).all()
    print('usercoupons:', usercoupons)
    for item in usercoupons:
        coupon_sn = item.coupon_sn
        couponuser = Coupon().query.filter(Coupon.coupon_sn == coupon_sn).first()
        if item.status == 0:
            couponuser.text = '去使用'
            couponuser.coupon_status = 1
        elif item.status == 1:
            couponuser.text = '已使用'
            couponuser.coupon_status = 2
        else:
            couponuser.text = '已过期'
            couponuser.coupon_status = 3

        couponlist.append(couponuser)

    return coupon_api(usercoupon=model_to_dicts(CouponOutSchema, data=couponlist), msg='获取成功')



@usercoupon.route('/delusercoupon',methods=["POST","GET"])
@jwt_required()
def delusercoupon():
    req_json = request.json
    openid = xss_escape(req_json.get('openid'))
    coupon_sn = xss_escape(req_json.get('coupon_sn'))
    coupon_user=CouponUser.query.filter(and_(CouponUser.coupon_sn==coupon_sn,CouponUser.openid==openid,CouponUser.status==0)).first()
    if coupon_user:
        coupon_user.status=1
        db.session.commit()
    return coupon_api(msg='操作成功')